/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase - Ticket
 * <p>Powered by webrx On 2021-12-24 09:26:28
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ticket implements Runnable {
    private int num;


    public Ticket() {
    }

    public Ticket(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        String n = Thread.currentThread().getName();
        while (true) {
            synchronized (this) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (num > 0) {
                    System.out.printf("%s:[售出%d号票，还剩余%d张票]%n", n, num--, num);
                } else {
                    break;
                }
            }
        }
    }
}
